package com.flame.core.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flame.core.protocol.Result;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FlameErrorController implements ErrorController {

    @RequestMapping("/error")
    public Result error(HttpServletRequest request, HttpServletResponse response) {
        // 返回异常信息
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
        return Result.failure().msg(httpStatus.getReasonPhrase()).code(String.valueOf(statusCode));
    }
}
